/*
 * @Descripttion: 
 * @version: 
 * @Author: MichaelHu
 * @Date: 2022-01-06 13:28:51
 * @LastEditors: MichaelHu
 * @LastEditTime: 2022-01-06 20:14:53
 */
#include "lauxlib.h"
#include "lua.h"
#include "lua_obj.h"
#include "lvgl/lvgl.h"

static int scr_act(lua_State *L)
{
    lv_obj_t *lv_obj = lv_img_create(lv_scr_act());
    lua_pushlightuserdata(L, (void *)lv_obj);
    return 1;
}

static int lua_lv_img_set_src(lua_State *L)
{
    lv_obj_t *lv_obj = (lv_obj_t *)lua_touserdata(L, 1);

    size_t l;
    const char *s = luaL_checklstring(L, 2, &l);
    lv_img_set_src(lv_obj, s);
    return 1;
}

static int help(lua_State *L);
static const struct luaL_Reg lv_obj_Lib[] = {
    {"scr_act", scr_act},
    {"img_set_src", lua_lv_img_set_src},
    {"center", lua_lv_obj_center},
    {"align", lua_lv_obj_align},
    {"size", lua_lv_obj_size},
    {"width", lua_lv_obj_set_width},
    {"height", lua_lv_obj_set_height},
    {"pos", lua_lv_obj_set_pos},
    {"x", lua_lv_obj_set_x},
    {"y", lua_lv_obj_set_y},
    {"obj_del", lua_lv_obj_del},
    {"help", help},
    {NULL, NULL}};

static int help(lua_State *L)
{
    uint16_t n = sizeof(lv_obj_Lib) / sizeof(struct luaL_Reg);

    for (int i = 0; i < n - 1; i++)
    {
        printf("%s\r\n", lv_obj_Lib[i].name);
    }
    return 1;
}

LUALIB_API int lua_lv_img_funinit(lua_State *L)
{
    luaL_newlib(L, lv_obj_Lib);
    lua_pushnumber(L, LV_ALIGN_CENTER);
    lua_setfield(L, -2, "LV_ALIGN_CENTER");
    return 1;
}
